package teach.aistar.controller;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import teach.aistar.model.entity.User;
import teach.aistar.model.vo.Result;
import teach.aistar.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class RegisterController {

    @GetMapping("/login")
    public String register() {
        return "login/login";
    }

    @Autowired
    IUserService userService;

    @PostMapping("/reg")
    @ResponseBody
    public Result register(@Validated User user, BindingResult bindingResult, HttpServletRequest req,HttpSession session) {

            List<FieldError> fieldErrorList = bindingResult.getFieldErrors();

            Map<String, String> errorsMap = new HashMap<>();
            if (null != fieldErrorList && fieldErrorList.size() > 0) {
                for (FieldError f : fieldErrorList) {
                    System.out.print(f.getField() + "=>");
                    System.out.println(f.getDefaultMessage());
                    errorsMap.put(f.getField(), f.getDefaultMessage());
                }
                return new Result("500", "登录失败", errorsMap);
            } else {
                String uname = user.getUname();
                String pwd = user.getPwd();
                System.out.println(uname);
                System.out.println(pwd);
                User user1 = userService.login(uname);
                System.out.println(user1);

                if (user1 != null) {
                    if (user1.getPwd().equals(pwd)) {
                        System.out.println(user1.getPwd());
                        System.out.println(pwd);
//                    model.addAttribute("uname",user1.getUname());
                        req.getSession().setAttribute("uname", user1.getUname());
                        req.setAttribute("uname", user1.getUname());
                        req.setAttribute("uId", user1.getUId());
                        session.setAttribute("User", user1);
                        return new Result("200", "登录成功", null);
                    } else {
                        return new Result("401", "密码错误", null);
                    }
                } else {
                    return new Result("402", "用户名错误", null);
                }
            }
        }}
//    @GetMapping("indexlogin")
//    public String index1(){
//        return "/orange/about";
//    }

//    @PostMapping("/reg")
//    @ResponseBody
//    public Result register(@Validated User user, BindingResult bindingResult){
//         List<FieldError> fieldErrorList = bindingResult.getFieldErrors();
//
//         Map<String,String> map = new HashMap<>();
//         if (null != fieldErrorList && fieldErrorList.size() >0){
//             for (FieldError f : fieldErrorList) {
//                 System.out.print(f.getField()+"=>");
//                 System.out.println(f.getDefaultMessage());
//                 map.put(f.getField(),f.getDefaultMessage());
//
//             }
//
//
//             return new Result("500","登录失败",map);
//
//         }
//         return new Result("200","登录成功",null);
//    }



